package com.ruyuan.payment.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruyuan.payment.server.domain.*;
import com.ruyuan.payment.server.enums.BillStatusEnum;
import com.ruyuan.payment.server.mapper.BillAlipayMapper;
import com.ruyuan.payment.server.mapper.BillHandleMapper;
import com.ruyuan.payment.server.mapper.BillWxpayMapper;
import com.ruyuan.payment.server.mapper.OrderPayMapper;
import com.ruyuan.payment.server.req.BillHandleQueryReq;
import com.ruyuan.payment.server.resp.BillHandleQueryResp;
import com.ruyuan.payment.server.resp.PageResp;
import com.ruyuan.payment.server.util.CopyUtil;
import com.ruyuan.payment.server.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BillHandleService {

    private static final Logger LOG = LoggerFactory.getLogger(BillHandleService.class);

    @Resource
    private OrderPayMapper orderPayMapper;

    @Resource
    private BillAlipayMapper billAlipayMapper;

    @Resource
    private BillWxpayMapper billWxpayMapper;

    @Resource
    private BillHandleMapper billHandleMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<BillHandleQueryResp> queryList(BillHandleQueryReq req) {
        PageHelper.startPage(req.getPage(), req.getSize());
        BillHandleExample billHandleExample = new BillHandleExample();
        billHandleExample.setOrderByClause("id desc");

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<BillHandle> billHandleList = billHandleMapper.selectByExample(billHandleExample);

        PageInfo<BillHandle> pageInfo = new PageInfo<>(billHandleList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<BillHandleQueryResp> queryRespList = CopyUtil.copyList(billHandleList, BillHandleQueryResp.class);

        PageResp<BillHandleQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(queryRespList);

        return pageResp;
    }

    public void doWrongBillAliPay(BillBatch billBatch, OrderPay orderPayDB, BillAlipay billAlipayDB, String billStatus) {
        Date now = new Date();
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            LOG.info("错账处理：支付订单号：{}，对账状态：{}", orderPayDB.getPayNo(), billStatus);
        }
        if (!ObjectUtils.isEmpty(billAlipayDB)) {
            LOG.info("错账处理：支付宝订单号：{}，对账状态：{}", billAlipayDB.getPayNo(), billStatus);
        }
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            LOG.info("更新支付订单表的对账状态，只更新关心的几个字段");
            OrderPay orderPayToUpdate = new OrderPay();
            orderPayToUpdate.setId(orderPayDB.getId());
            orderPayToUpdate.setBillBatchNo(billBatch.getBatchNo());
            orderPayToUpdate.setBillStatus(billStatus);
            orderPayToUpdate.setUpdatedTime(now);
            orderPayMapper.updateByPrimaryKeySelective(orderPayToUpdate);
        }

        if (!ObjectUtils.isEmpty(billAlipayDB)) {
            LOG.info("更新支付宝对账表的对账状态，只更新关心的几个字段");
            BillAlipay billAlipayToUpdate = new BillAlipay();
            billAlipayToUpdate.setId(billAlipayDB.getId());
            billAlipayToUpdate.setBillStatus(billStatus);
            billAlipayToUpdate.setUpdatedTime(now);
            billAlipayMapper.updateByPrimaryKeySelective(billAlipayToUpdate);
        }

        LOG.info("如果差错表中已有差错订单，则先删除");
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            BillHandleExample billHandleExample = new BillHandleExample();
            billHandleExample.createCriteria().andPayNoEqualTo(orderPayDB.getPayNo());
            billHandleMapper.deleteByExample(billHandleExample);
        }

        if (!ObjectUtils.isEmpty(billAlipayDB)) {
            BillHandleExample billHandleExample = new BillHandleExample();
            billHandleExample.createCriteria().andChannelPayNoEqualTo(billAlipayDB.getPayNo());
            billHandleMapper.deleteByExample(billHandleExample);
        }

        LOG.info("新增一条差错记录");
        BillHandle billHandle = new BillHandle();
        billHandle.setId(snowFlake.nextId());
        billHandle.setBatchNo(billBatch.getBatchNo());
        billHandle.setBillDate(billBatch.getBillDate());
        billHandle.setChannel(billBatch.getChannel());
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            billHandle.setPayNo(orderPayDB.getPayNo());
            billHandle.setPayAmount(orderPayDB.getChannelAmount());
            billHandle.setPayStatus(orderPayDB.getStatus());
        }
        if (!ObjectUtils.isEmpty(billAlipayDB)) {
            billHandle.setChannelPayNo(billAlipayDB.getPayNo());
            billHandle.setChannelAmount(billAlipayDB.getAmountIn());
        }
        billHandle.setBillStatus(billStatus);
        billHandle.setCreatedTime(now);
        billHandle.setUpdatedTime(now);
        billHandleMapper.insert(billHandle);
    }

    public void doRightBillAliPay(BillBatch billBatch, OrderPay orderPayDB, BillAlipay billAlipayDB) {
        Date now = new Date();
        LOG.info("对账正常处理：支付订单号：{}", orderPayDB.getPayNo());

        LOG.info("更新支付订单表的对账状态为成功，只更新关心的几个字段");
        OrderPay orderPayToUpdate = new OrderPay();
        orderPayToUpdate.setId(orderPayDB.getId());
        orderPayToUpdate.setBillBatchNo(billBatch.getBatchNo());
        orderPayToUpdate.setBillStatus(BillStatusEnum.NORMAL.getCode());
        orderPayToUpdate.setUpdatedTime(now);
        orderPayMapper.updateByPrimaryKeySelective(orderPayToUpdate);

        LOG.info("更新支付宝对账表的对账状态为成功，只更新关心的几个字段");
        BillAlipay billAlipayToUpdate = new BillAlipay();
        billAlipayToUpdate.setId(billAlipayDB.getId());
        billAlipayToUpdate.setBillStatus(BillStatusEnum.NORMAL.getCode());
        billAlipayToUpdate.setUpdatedTime(now);
        billAlipayMapper.updateByPrimaryKeySelective(billAlipayToUpdate);

        LOG.info("如果差错表中已有差错订单，则先删除");
        BillHandleExample billHandleExample = new BillHandleExample();
        billHandleExample.createCriteria().andPayNoEqualTo(orderPayDB.getPayNo());
        billHandleMapper.deleteByExample(billHandleExample);

        billHandleExample = new BillHandleExample();
        billHandleExample.createCriteria().andChannelPayNoEqualTo(billAlipayDB.getPayNo());
        billHandleMapper.deleteByExample(billHandleExample);
    }

    public void doWrongBillWxPay(BillBatch billBatch, OrderPay orderPayDB, BillWxpay billWxpayDB, String billStatus) {
        Date now = new Date();
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            LOG.info("错账处理：支付订单号：{}，对账状态：{}", orderPayDB.getPayNo(), billStatus);
        }
        if (!ObjectUtils.isEmpty(billWxpayDB)) {
            LOG.info("错账处理：微信订单号：{}，对账状态：{}", billWxpayDB.getPayNo(), billStatus);
        }
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            LOG.info("更新支付订单表的对账状态，只更新关心的几个字段");
            OrderPay orderPayToUpdate = new OrderPay();
            orderPayToUpdate.setId(orderPayDB.getId());
            orderPayToUpdate.setBillBatchNo(billBatch.getBatchNo());
            orderPayToUpdate.setBillStatus(billStatus);
            orderPayToUpdate.setUpdatedTime(now);
            orderPayMapper.updateByPrimaryKeySelective(orderPayToUpdate);
        }

        if (!ObjectUtils.isEmpty(billWxpayDB)) {
            LOG.info("更新微信对账表的对账状态，只更新关心的几个字段");
            BillWxpay billWxpayToUpdate = new BillWxpay();
            billWxpayToUpdate.setId(billWxpayDB.getId());
            billWxpayToUpdate.setBillStatus(billStatus);
            billWxpayToUpdate.setUpdatedTime(now);
            billWxpayMapper.updateByPrimaryKeySelective(billWxpayToUpdate);
        }

        LOG.info("如果差错表中已有差错订单，则先删除");
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            BillHandleExample billHandleExample = new BillHandleExample();
            billHandleExample.createCriteria().andPayNoEqualTo(orderPayDB.getPayNo());
            billHandleMapper.deleteByExample(billHandleExample);
        }

        if (!ObjectUtils.isEmpty(billWxpayDB)) {
            BillHandleExample billHandleExample = new BillHandleExample();
            billHandleExample.createCriteria().andChannelPayNoEqualTo(billWxpayDB.getPayNo());
            billHandleMapper.deleteByExample(billHandleExample);
        }

        LOG.info("新增一条差错记录");
        BillHandle billHandle = new BillHandle();
        billHandle.setId(snowFlake.nextId());
        billHandle.setBatchNo(billBatch.getBatchNo());
        billHandle.setBillDate(billBatch.getBillDate());
        billHandle.setChannel(billBatch.getChannel());
        if (!ObjectUtils.isEmpty(orderPayDB)) {
            billHandle.setPayNo(orderPayDB.getPayNo());
            billHandle.setPayAmount(orderPayDB.getChannelAmount());
            billHandle.setPayStatus(orderPayDB.getStatus());
        }
        if (!ObjectUtils.isEmpty(billWxpayDB)) {
            billHandle.setChannelPayNo(billWxpayDB.getPayNo());
            billHandle.setChannelAmount(billWxpayDB.getAmountIn());
        }
        billHandle.setBillStatus(billStatus);
        billHandle.setCreatedTime(now);
        billHandle.setUpdatedTime(now);
        billHandleMapper.insert(billHandle);
    }

    public void doRightBillWxPay(BillBatch billBatch, OrderPay orderPayDB, BillWxpay billWxpayDB) {
        Date now = new Date();
        LOG.info("对账正常处理：支付订单号：{}", orderPayDB.getPayNo());

        LOG.info("更新支付订单表的对账状态为成功，只更新关心的几个字段");
        OrderPay orderPayToUpdate = new OrderPay();
        orderPayToUpdate.setId(orderPayDB.getId());
        orderPayToUpdate.setBillBatchNo(billBatch.getBatchNo());
        orderPayToUpdate.setBillStatus(BillStatusEnum.NORMAL.getCode());
        orderPayToUpdate.setUpdatedTime(now);
        orderPayMapper.updateByPrimaryKeySelective(orderPayToUpdate);

        LOG.info("更新微信对账表的对账状态为成功，只更新关心的几个字段");
        BillWxpay billWxpayToUpdate = new BillWxpay();
        billWxpayToUpdate.setId(billWxpayDB.getId());
        billWxpayToUpdate.setBillStatus(BillStatusEnum.NORMAL.getCode());
        billWxpayToUpdate.setUpdatedTime(now);
        billWxpayMapper.updateByPrimaryKeySelective(billWxpayToUpdate);

        LOG.info("如果差错表中已有差错订单，则先删除");
        BillHandleExample billHandleExample = new BillHandleExample();
        billHandleExample.createCriteria().andPayNoEqualTo(orderPayDB.getPayNo());
        billHandleMapper.deleteByExample(billHandleExample);

        billHandleExample = new BillHandleExample();
        billHandleExample.createCriteria().andChannelPayNoEqualTo(billWxpayDB.getPayNo());
        billHandleMapper.deleteByExample(billHandleExample);
    }
}
